package com.lxq.particledemo02;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;


import android.content.Context;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

public class ParticleShaderProgram {

    protected static final String U_TIME = "u_Time";
    protected static final String U_MATRIX = "u_Matrix";
    protected static final String A_POSITION = "a_Position";
    protected static final String A_COLOR = "a_Color";
    protected static final String A_DIRECTION_VECTOR = "a_DirectionVector";
    protected static final String A_PARTICLE_START_TIME = "a_ParticleStartTime";
    //protected static final String U_TEXTURE = "u_TextureUnit";

    // Uniformlocations
    private int uMatrixLocation;
    private int uTimeLocation;
    // Attributelocations
    private int aPositionLocation;
    private int aColorLocation;
    private int aDirectionVectorLocation;
    private int aParticleStartTimeLocation;
    //private int uTextureLocation;

    private String mVertexShader;
    private String mFragmentShader;

    int mProgram;


    public ParticleShaderProgram(Context context, GLSurfaceView mv) {
        initShader(mv);
    }


    public void initShader(GLSurfaceView mv) {

        mVertexShader = ShaderUtil.loadFromAssetsFile("vertex.glsl", mv.getResources());
        mFragmentShader = ShaderUtil.loadFromAssetsFile("frag.glsl", mv.getResources());


        mProgram = ShaderUtil.createProgram(mVertexShader, mFragmentShader);


        uMatrixLocation = GLES20.glGetUniformLocation(mProgram, U_MATRIX);
        uTimeLocation = GLES20.glGetUniformLocation(mProgram, U_TIME);

        aPositionLocation = GLES20.glGetAttribLocation(mProgram, A_POSITION);
        aColorLocation = GLES20.glGetAttribLocation(mProgram, A_COLOR);
        aDirectionVectorLocation = GLES20.glGetAttribLocation(mProgram,
                A_DIRECTION_VECTOR);
        aParticleStartTimeLocation = GLES20.glGetAttribLocation(mProgram,
                A_PARTICLE_START_TIME);


        //uTextureLocation = GLES20.glGetUniformLocation(mProgram, U_TEXTURE);

    }

    public void useProgram() {
        GLES20.glUseProgram(mProgram);
    }

    public void setUniforms(float[] matrix, float elapsedTime, int textureId) {
        GLES20.glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);
        GLES20.glUniform1f(uTimeLocation, elapsedTime);

        /*GLES20.glEnable( );
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        GLES20.glUniform1i(uTextureLocation, 0);*/
    }

    public int getPositionAttributeLocation() {
        return aPositionLocation;
    }

    public int getColorAttributeLocation() {
        return aColorLocation;
    }

    public int getDirectionVectorAttributeLocation() {
        return aDirectionVectorLocation;
    }

    public int getParticleStartTimeAttributeLocation() {
        return aParticleStartTimeLocation;
    }

}